package com.gykj.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.gykj.admin.common.entity.Resources;
import com.gykj.admin.service.ResourcesService;
import com.gykj.admin.shiro.ShiroService;

/**
 * Created by rohto.li on 2017/11/03.
 */
@RestController
@RequestMapping("/resources")
public class ResourcesController {

    @Resource
    private ResourcesService resourcesService;
    @Resource
    private ShiroService shiroService;

    @RequestMapping
    public Map<String,Object> getAll(Resources resources, String draw,
                                     @RequestParam(required = false, defaultValue = "1") int start,
                                     @RequestParam(required = false, defaultValue = "10") int length){
        Map<String,Object> map = new HashMap<>();
        PageInfo<Resources> pageInfo = resourcesService.selectByPage(resources, start, length);
        System.out.println("pageInfo.getTotal():"+pageInfo.getTotal());
        map.put("draw",draw);
        map.put("recordsTotal",pageInfo.getTotal());
        map.put("recordsFiltered",pageInfo.getTotal());
        map.put("data", pageInfo.getList());
        return map;
    }

    @RequestMapping(value = "/resourcesWithSelected")
    public List<Resources> resourcesWithSelected(@RequestParam(value = "rid",required = false) Integer rid) {
        return resourcesService.queryResourcesListWithSelected(rid);
    }
    
    @RequestMapping(value = "/getResourcesById")
    public List<Resources> getResourcesById(@RequestParam(value = "rid",required = false) Integer rid) {
        return resourcesService.queryResourcesListWithSelected(rid);
    }
    
    @RequestMapping(value = "/getAllResourcesList")
    public  PageInfo<Resources> getAllResourcesList(
    		@RequestBody Resources resources,
            @RequestParam(value="currentPage",required = false, defaultValue = "1") int currentPage,
            @RequestParam(value="pageSize",required = false, defaultValue = "20") int pageSize,
            @RequestParam(value="sortColumn",required = false, defaultValue = "1") int sortColumn,
            @RequestParam(value="sortDesc",required = false, defaultValue = "1") int sortDesc
    		)
    {
        return resourcesService.selectByPage(resources, pageSize, sortColumn);
    }

/*    @RequestMapping(value = "/loadMenu",method=RequestMethod.GET)
    public List<Resources> loadMenu( Integer uid){
        Map<String,Object> map = new HashMap<>();
        Integer userid = (Integer) SecurityUtils.getSubject().getSession().getAttribute("userSessionId");
        map.put("type",1);
        //方便其他模块调用时候，session 中取不到用户ID
        if (userid == null ||userid  == 0) {
        	userid =  uid;
        }
        map.put("userid",userid);
        
        List<Resources> resourcesList = resourcesService.loadUserResources(map);
        return resourcesList;
    }*/
    
    @RequestMapping(value = "/loadMenu/{uid}",method=RequestMethod.GET)
    public List<Resources> loadMenu(@PathVariable(value="uid",required=false) Integer uid){
        Map<String,Object> map = new HashMap<>();
        Integer userid = (Integer) SecurityUtils.getSubject().getSession().getAttribute("userSessionId");
        map.put("type",1);
        //方便其他模块调用时候，session 中取不到用户ID
        if (userid == null ||userid  == 0) {
        	userid =  uid;
        }
        map.put("userid",userid);
        
        List<Resources> resourcesList = resourcesService.loadUserResources(map);
        return resourcesList;
    }

    //@CacheEvict(cacheNames="resources", allEntries=true)
    @RequestMapping(value = "/add")
    public String add(@RequestBody Resources resources){
        try{
            resourcesService.save(resources);
            //更新权限
            shiroService.updatePermission();
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "fail";
        }
    }
    @CacheEvict(cacheNames="resources", allEntries=true)
    @RequestMapping(value = "/delete")
    public String delete(@RequestParam(value="id",required=false) Integer id){
        try{
            resourcesService.delete(id);
            //更新权限
            shiroService.updatePermission();
            return "success";
        }catch (Exception e){
            e.printStackTrace();
            return "fail";
        }
    }
}
